package com.liang.oss.adapter;

import com.liang.oss.entity.FileInfo;
import com.liang.oss.utils.MinioUtil;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

public class MinioStorageAdapter implements StorageAdapter {

    @Resource
    private MinioUtil minioUtil;

    @Override
    @SneakyThrows
    public void createBucket(String bucketName) {
        minioUtil.createBucket(bucketName);
    }

    @Override
    @SneakyThrows
    public void upload(MultipartFile file, String bucketName, String objectName) {
        minioUtil.createBucket(bucketName);
        if (objectName != null) {
            InputStream inputStream = file.getInputStream();
            minioUtil.upload(inputStream, bucketName, objectName + "/" + file.getOriginalFilename());
        } else {
            minioUtil.upload(file.getInputStream(), bucketName, file.getOriginalFilename());
        }
    }

    @Override
    @SneakyThrows
    public InputStream download(String bucketName, String objectName) {
        return minioUtil.download(bucketName, objectName);
    }

    @Override
    @SneakyThrows
    public List<String> listBuckets() {
        return minioUtil.listBuckets();
    }

    @Override
    @SneakyThrows
    public List<FileInfo> listObjects(String bucketName) {
        return minioUtil.listObjects(bucketName);
    }

    @Override
    @SneakyThrows
    public void removeBucket(String bucketName) {
        minioUtil.removeBucket(bucketName);
    }

    @Override
    @SneakyThrows
    public void removeObject(String bucketName, String objectName) {
        minioUtil.removeObject(bucketName, objectName);
    }

    @Override
    public String getUrl(String bucketName, String objectName) {
        return "";
    }
}
